package com.example.roundcornerprogressbar.view.progressbar.indeterminate;

import com.example.roundcornerprogressbar.view.progressbar.RoundCornerProgressBar;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class IndeterminateRoundCornerProgressBar extends RoundCornerProgressBar {
    private HiLogLabel label = new HiLogLabel(HiLog.LOG_APP,1, "RoundCorner:IndeterminateRoundCornerProgressBar");

    public IndeterminateRoundCornerProgressBar(Context context) {
        super(context);
    }

    public IndeterminateRoundCornerProgressBar(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    public IndeterminateRoundCornerProgressBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    @Override
    protected void initView() {
        super.initView();
        setMax(100);
    }

//    @Override
//    protected void onViewDraw() {
//        TaskDispatcher dispatcher = getContext().getUITaskDispatcher();
//        dispatcher.asyncDispatch(new Runnable() {
//            @Override
//            public void run() {
//                int visibility = getVisibility();
//                if (visibility == Component.VISIBLE) {
//                    startIndeterminateAnimation();
//                }
//            }
//        });
//    }

    @Override
    protected void onProgressChangeAnimationUpdate(DirectionalLayout layout, float current, float to) {
        HiLog.info(label, "onProgressChangeAnimationUpdate");
        super.onProgressChangeAnimationUpdate(layout, current, to);
        HiLog.info(label, "isComponentDisplayed = "+isComponentDisplayed());
        if (!isComponentDisplayed()) {
            super.stopProgressAnimationImmediately();
        }
    }

    @Override
    protected void onProgressChangeAnimationEnd(DirectionalLayout layout) {
        HiLog.info(label, "onProgressChangeAnimationEnd");
        if (isComponentDisplayed()) {
            startIndeterminateAnimation();
        }
    }

    public void startIndeterminateAnimation() {
        HiLog.info(label, "startIndeterminateAnimation");
        disableAnimation();
        setProgress(0);
        enableAnimation();
        setProgress(100);
    }

    public void stopIndeterminateAnimation() {
        super.stopProgressAnimationImmediately();
    }
}
